<?php

namespace QF;

class Admin extends \Model {
    
    use \UserModel;
    
    protected $_id = 'uid';
    static public $table = 'admin';
    protected $_fillable = [
        'uid' => 'int',
        'role_ids' => 'str',
        'username' => 'str',
        'realname' => 'str',
        'password' => 'str',
        'nickname' => 'str',
        'locked' => 'int',
        'password_changed_at' => 'datetime',
    ];
    private $_md5key;
    
    public function __construct() {
        $this->_md5key = \Sys::getCfg('admin_crypt_key');
    }
    
    public function getRoleIds($uid) {
        return explode(',', trim($this->get($uid, 'role_ids'), ','));
    }
    
    public function add($data) {
        if (!$data['nickname']) {
            $data['nickname'] = filter_utf8mb4($data['username']);
        } else {
            $data['nickname'] = filter_utf8mb4($data['nickname']);
        }
        
        if ($data['password']) {
            $data['password'] = cmd5($data['password'], $this->_md5key);
        }
        
        if (is_array($data['role_ids'])) {
            $data['role_ids'] = implode(',', $data['role_ids']);
        }
        
        return parent::add($data);
    }
    
    public function update($uid, $data) {
        if ($data['password']) {
            $data['password'] = cmd5($data['password'], $this->_md5key);
        } else {
            unset($data['password']);
        }
        
        if ($data['nickname']) {
            $data['nickname'] = filter_utf8mb4($data['nickname']);
        }
        
        if (is_array($data['role_ids'])) {
            $data['role_ids'] = implode(',', $data['role_ids']);
        }
        
        return parent::update($uid, $data);
    }
    
    public function updatePassword($id, $old, $new) {
        $where = array(
            $this->_id => $id,
            'password' => cmd5($old, $this->_md5key)
        );
        
        $password = $this->get($id, 'password');
        
        if ($password == cmd5($new, $this->_md5key)) {
            return true;
        }
        
        $data = array('password' => cmd5($new, $this->_md5key));
        
        $result = Db()->update(static::$table, $data, $where);
        
        if ($result) {
            return Db()->affectedRows();
        }
        return $result;
    }
    
    public function updatePasswordForce($id, $new) {
        $where = array($this->_id => $id);
        
        $data = array('password' => cmd5($new, $this->_md5key));
        
        return Db()->update(static::$table, $data, $where);
    }
    
    /**
     * 验证用户是否通过授权
     */
    public function checkLogin($username, $password) {
        $where = array(
            'username' => $username,
            'password' => cmd5($password, $this->_md5key),
            'locked' => 0,
        );
        return Db()->getOne(static::$table)->fields($this->_id)->where($where)->result();
    }
    
    public function searchUids($keyword) {
        $keyword = Db()->escape($keyword, false);
        $where = "username like '%{$keyword}%' or realname like '%{$keyword}%' or nickname like '%{$keyword}%'";
        return Db()->getCol(static::$table)->fields('uid')->where($where)->result();
    }
    
    public function searchUid($keyword) {
        $keyword = Db()->escape($keyword, false);
        $where = "username like '%{$keyword}%' or realname like '%{$keyword}%' or nickname like '%{$keyword}%'";
        return Db()->getOne(static::$table)->fields('uid')->where($where)->result();
    }
    
    public function countList($filter = array(), $filter2 = '') {
        return Db()->count(static::$table)->where($filter)->where($filter2)->result();
    }
    
    public function getList($offset = null, $length = null, $filter = array(), $filter2 = '', $order = '') {
        $order || $order = "{$this->_id} desc";
        return Db()->getAll(static::$table)->where($filter)->where($filter2)->limit($offset, $length)->order($order)->result();
    }
    
    public function getUidByUsername($username) {
        $where = array('username' => $username);
        $result = Db()->getOne(static::$table)->fields($this->_id)->where($where)->result();
        return $result;
    }
    
    public function lockOne($uid) {
        $where = array($this->_id => $uid);
        $arrSets = array('locked' => 1);
        $result = Db()->update(static::$table, $arrSets, $where);
        return $result;
    }
    
    public function unlockOne($uid) {
        $where = array($this->_id => $uid);
        $arrSets = array('locked' => 0);
        $result = Db()->update(static::$table, $arrSets, $where);
        return $result;
    }
    
}
